package com.ittx.dzt.tiktok.service;

import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import open.dto.tiktok.TiktokPackageDs;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.json.JacksonTool;
import com.ittx.dzt.tiktok.constant.TiktokApiConstant;
import com.ittx.dzt.tiktok.handler.AbstractBaseTiktokApiHandler;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 查询可用承运商
 * <p>
 * cy.tiktok.order.shippingService.get
 * /fulfillment/202309/orders/{order_id}/shipping_services/query （查询可用承运商） https://partner.tiktokshop.com/docv2/page/650aa6b2bace3e02b75dda4e?external_id=650aa6b2bace3e02b75dda4e
 * </p>
 *
 * @author gywang
 * @since 2024-07-26
 */
@Slf4j
@Service
public class TiktokOrderShippingServiceGetService extends AbstractBaseTiktokApiHandler<TiktokPackageDs> {
    @Override
    public String getTiktokMethod() {
        return TiktokApiConstant.ORDER_SHIPPING_SERVICE_GET;
    }

    @Override
    public String getLocalApiMethod() {
        return PlatformApiConstant.InboundInterface.TIKTOK_ORDER_SHIPPING_SERVICE_GET;
    }

    @Override
    public Result doProcess(PlatformApiParamDTO platformApiParamDTO, TiktokPackageDs tiktokPackageDimensionDs) {
        Result<?> validResult = checkShopCipherInfo(platformApiParamDTO);
        if (validResult.hasError()) {
            return validResult;
        }
        String url = getDefaultUrl(platformApiParamDTO.getStoreConfig());
        url = StringUtils.replace(url, "{order_id}", tiktokPackageDimensionDs.getSourceOrderCode());
        Map<String, Object> params = new HashMap<>();
        params.put("shop_cipher", getShopCipher(platformApiParamDTO));
        Map<String, Object> reqBody = new HashMap<>();
        reqBody.put("order_line_item_ids", tiktokPackageDimensionDs.getSourceLineNums());
        Map<String, Object> weight = new HashMap<>();
        weight.put("value", tiktokPackageDimensionDs.getWeight());
        weight.put("unit", tiktokPackageDimensionDs.getWeightUnit());
        reqBody.put("weight", weight);
        Map<String, Object> dimension = new HashMap<>();
        dimension.put("length", tiktokPackageDimensionDs.getLength());
        dimension.put("width", tiktokPackageDimensionDs.getWidth());
        dimension.put("height", tiktokPackageDimensionDs.getHeight());
        dimension.put("unit", tiktokPackageDimensionDs.getDimensionUnit());
        reqBody.put("dimension", dimension);

        return this.executeTiktokApi(url, platformApiParamDTO.getStoreConfig(), params, JacksonTool.toJson(reqBody), platformApiParamDTO.getRequestId(), TiktokApiConstant.ApiHttpMethod.POST);
    }
}
